package com.police.activity;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.astrob.ESurfing.Util.StringUtil;
import com.astrob.mapapi.core.PoiItem;
import com.astrob.mapapi.util.BitmapUtil;
import com.police.R;

import java.util.List;

public class PoiAdapter extends BaseAdapter {

	private Context context = null;
	private List<PoiItem> dataList = null;

	public PoiAdapter(Context context, final List<PoiItem> data) {
		this.context = context;
		this.dataList = data;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		if (null != dataList)
			return dataList.size();

		return 0;
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		if (null != dataList)
			return dataList.get(position);
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder holder;
		if (null == convertView) {
			convertView = LinearLayout
					.inflate(context, R.layout.poi_item, null);

			holder = new ViewHolder();

			holder.typeView = (ImageView) convertView
					.findViewById(R.id.sresult_type_view);
			holder.dirView = (ImageView) convertView
					.findViewById(R.id.sresult_direct_view);

			holder.nameView = (TextView) convertView
					.findViewById(R.id.sresult_name_view);
			holder.addressView = (TextView) convertView
					.findViewById(R.id.sresult_address_view);
			holder.disView = (TextView) convertView
					.findViewById(R.id.sresult_dis_view);

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		PoiItem data = dataList.get(position);

		//holder.typeView.setImageDrawable(getDrawable(position % getCount()));
		holder.nameView.setText(StringUtil.trimToEmpty(data.getTitle()));

		String strTemp = StringUtil.trimToEmpty(data.getSnippet());

		holder.addressView.setText(strTemp);
		// holder.disView.setText(MapSearchService.getLength(data.getCoordinate(),
		// centerPos));
		return convertView;
	}

	private Drawable getDrawable(int i) {
		char[] arrayOfChar1 = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
				'j' };
		// char[] arrayOfChar2 = { 'l', 'm', 'h' };

		Drawable localDrawable = null;
		if (i < 10) {
			StringBuilder filename = new StringBuilder(32);
			filename.append("icon_mark").append(arrayOfChar1[i]).append('_')
					.append('h').append(".png");
			localDrawable = BitmapUtil.getDrawableFromAssets(context,
					filename.toString());
		}

		return localDrawable;
	}

	static class ViewHolder {
		ImageView typeView;
		ImageView dirView;

		TextView nameView;
		TextView addressView;
		TextView disView;
	}

}
